#!/bin/sh
#
# description: Starts and stops the MediaRSS
#
mos=/usr/local/etc/mos/mediarss
momenu=/usr/local/etc/moMenu.rss

case "$1" in
  start)
	echo "Starting MediaRSS..."
	cp -Rfp $mos/www/* /tmp/www/
	#[ ! -e /usr/local/etc/sayler ] && ln -s $mos/sayler/ /usr/local/etc/sayler
	;;
  stop)
	echo "Stopping MediaRSS..."
	rm -f /tmp/www/cgi-bin/rss
	;;

  status)
	if [ -e /tmp/www/cgi-bin/rss ] ; then
	  echo "MediaRSS is running"
	else
	  echo "MediaRSS is stopped"
	fi
	;;

  enable)
	echo "Enabling MediaRSS..."

	# copying files
	cp -fp  $mos/sayler_module /usr/local/etc/
	ln -s $mos/sayler/ /usr/local/etc/sayler
	[ ! -e /usr/local/etc/sayler.list ] && cp -fp $mos/sayler.list /usr/local/etc/

	if [ ! -d /usr/local/etc/playlist ] ; then
	  mkdir /usr/local/etc/playlist
	  cp -fp $mos/playlist/* /usr/local/etc/playlist/
	fi

	# adding to RSS
	if [ ! -f $momenu ] ; then
	  cp /usr/local/etc/mos/core/rss/moMenu.rss /usr/local/etc/
	fi

	if [ -z "$( cat $momenu | grep etc/sayler )" ] ; then
	  sed -i '
/<title>moServices<\/title>/i\
		<title>M3U playlist</title>\
		<link>http://127.0.0.1/cgi-bin/rss</link>\
		<media:thumbnail url="/usr/local/etc/sayler/sayler.png" width="120" height="90" />\
	</item>\
	<item>
' $momenu
	fi
	;;

  disable)
	echo "Disabling MediaRSS..."

	# Remove from RSS
	sed -i '
/<title>M3U playlist<\/title>/{N;N;N;N;d}
' $momenu

	# Remove files
	rm -f /usr/local/etc/sayler_module
	rm -f /usr/local/etc/sayler
	;;

  *)
	echo "Usage: $0 {start|stop|status|enable|disable}"
esac

exit $?
